package com.vm.adapter;

import java.util.List;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.vm.activity.DetailActivity;
import com.vm.commons.VMConst;
import com.vm.model.Collection;
import com.vm.utils.MyDisplayImageOptions;
import com.wwtscapp.apk.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

@SuppressLint("InflateParams")
public class CollectionAdapter1 extends BaseAdapter {
	private Context mContext;
	private List<Collection> listCollections;

	public CollectionAdapter1(Context context, List<Collection> lists) {
		this.mContext = context;
		this.listCollections = lists;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listCollections.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {

			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.collection_item, null);
			viewHolder = new ViewHolder();
			viewHolder.iv_softImage = (ImageView) convertView
					.findViewById(R.id.iv_softImage);
			viewHolder.rb_star = (RatingBar) convertView
					.findViewById(R.id.rb_star);
			viewHolder.tv_softName = (TextView) convertView
					.findViewById(R.id.tv_softName);
			viewHolder.tv_softSize = (TextView) convertView
					.findViewById(R.id.tv_softSize);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		Collection collection = listCollections.get(position);
		viewHolder.tv_softName.setTag(collection.getSoftId());
		if (collection.getName() != null) {
			viewHolder.tv_softName.setText(collection.getName());
		}
		if (collection.getSize() != null) {
			viewHolder.tv_softSize.setText(Float.toString(collection.getSize())
					+ "MB");
		}
		String uri = VMConst.URL + collection.getImageurl();
		ImageLoader.getInstance().displayImage(uri, viewHolder.iv_softImage,
				MyDisplayImageOptions.getInstance());
		if (collection.getStar() != null) {
			float fstar = collection.getStar();
			viewHolder.rb_star.setRating(fstar / 2);
		}
		convertView.setOnClickListener(lv_listener);
		return convertView;
	}

	OnClickListener lv_listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent = new Intent(v.getContext(), DetailActivity.class);
			TextView textView = (TextView) v.findViewById(R.id.tv_softName);
			String softId = textView.getTag().toString();
			intent.putExtra("softId", softId);
			v.getContext().startActivity(intent);
		}

	};

	static class ViewHolder {
		ImageView iv_softImage;
		TextView tv_softName;
		RatingBar rb_star;
		TextView tv_softSize;
	}
}